<?php
namespace app\api\controller;
// namespace app\zw_admin\controller;
use \think\Controller;
use think\Db;
// use think\Controller;
use think\Session;
use think\Request;
use Endroid\QrCode\Tests;
use qrcode;
use alisms\SmsDemo;
class GoodsController extends Controller
{

    /**
     * 首页限时特价
     * @return [type] [description]
     */
    public function goods_index()
    {
        
        $goodsList = db('activity_goods')->where(['off'=>1])->order('time desc')->limit(4)->select();
        $goodsArr = [];
        foreach ($goodsList as $key => $value) {
            $goodsArr[$key] = $value;
            $goodsArr[$key]['goods_img'] = explode(",",$value['goods_img']);
            foreach ($goodsArr[$key]['goods_img'] as $k => &$v) {
                // dump($v);
                $v = 'http://www.mochoushop.com/'.$v;
            }
           
        }
        $goodsListRes = [];
        foreach ($goodsArr as $key => $value) {
            $value['cover_img'] = 'http://www.mochoushop.com/'.$value['cover_img'];
            $goodsListRes[$key] = $value;
        }
        // exit;
        if (!empty($goodsList)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$goodsListRes));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }

    }





    /**
     * 限时特价详情列表
     * @return [type] [description]
     */
    public function goodsList()
    {
        $goodsList = db('activity_goods')->order('g_id desc')->select();
        $goodsArr = [];
        foreach ($goodsList as $key => $value) {
            $goodsArr[$key] = $value;
            $goodsArr[$key]['goods_img'] = explode(",",$value['goods_img']);
            foreach ($goodsArr[$key]['goods_img'] as $k => &$v) {
                // dump($v);
                $v = 'http://www.mochoushop.com/'.$v;
            }
        }
        $goodsListRes = [];
        foreach ($goodsArr as $key => $value) {
            $value['cover_img'] = 'http://www.mochoushop.com/'.$value['cover_img'];
            $goodsListRes[$key] = $value;
        }
        if (!empty($goodsList)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$goodsListRes));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }




    /**
     * 特价商品详情
     * @return [g_id] [商品id]
     */
    public function goods_details()
    {
        $g_id = input('param.gid');
        $goodsList = db('activity_goods')->where(['g_id'=>$g_id])->select();
        $goodsOffList = db('activity_goods')->where(['off'=>'1','g_id'=>['neq',$g_id]])->order('g_id desc')->limit(3)->select();
        $sellingList = db('selling')->where(['selling_id'=>$goodsList[0]['selling_id']])->find();
        $brandList = db('brand')->where(['brand_id'=>$goodsList[0]['brand_id']])->find();
        // dump($goodsOffList);
        // exit;
        $goodsArr = [];
        foreach ($goodsList as $key => $value) {
            $goodsArr['goods'] = $value;
            $goodsArr['goods']['selling_name'] = $sellingList['selling_name'];
            $goodsArr['goods']['brand_name'] = $brandList['brand_name'];
            $goodsArr['goods']['goods_img'] = explode(",",$value['goods_img']);
            $goodsArr['newgoods'] =  $goodsOffList;
            
            foreach ($goodsArr['goods']['goods_img'] as $k => &$v) {
                // dump($v);
                $v = 'http://www.mochoushop.com/'.$v;
            }
        }
        foreach ($goodsArr['newgoods'] as $key => $value) {
            $goodsArr['newgoods'][$key]['goods_img'] = explode(",",$value['goods_img']);
            
            // dump($value);
        }
        foreach ($goodsArr['newgoods'] as $k => &$v) {
            foreach ($v['goods_img'] as $key => &$value) {
                $value = 'http://www.mochoushop.com/'.$value;
                // dump($value);

            }
            // dump($v);
                
        }
        // dump($goodsArr);
        // exit;
        if (!empty($goodsList)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$goodsArr));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }


    /**
     * 展示收货地址
     * @return [type] [description]
     */
    public function address_list()
    {
        $uid = input('param.uid');
        $addressList = db('address')->where(['uid'=>$uid])->select();
        if (!empty($addressList)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$addressList));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }




    /**
     * 填写订单信息,并生成订单
     * @return [uid] [用户id]
     * @return [aid] [收货地址id]
     * @return [gid] [特价商品id]
     * @return [num] [数量]
     */
    public function goods_order()
    {
        $userList = input('param.');
        /**
         * 查询用户信息
         * @var [uid]
         */
        $userArr = db('member')->where(['uid'=>$userList['uid']])->find();
        $addressList = db('address')->where(['aid'=>$userList['aid']])->find();
        // $goodsList = db('activity_goods')->where(['g_id'=>$userList['gid']])->find();
        /**
         * 联锁查询商品及卖场信息
         * @var [type]
         */
        $goods_list = db('activity_goods')
        ->alias('a')
        ->join('drm_selling b','b.selling_id = a.selling_id')
        ->join('drm_brand c','c.brand_id = a.brand_id')
        ->where(['g_id'=>$userList['gid']])
        ->select();
        
        /**
         * 获取商品首张图片
         */
        $imgarr = explode(",",$goods_list[0]['goods_img']);
        $goods_img = $imgarr[0];
       
        // echo $goods_img;
        // exit;
        /**
         * 查询商户信息
         * @var [type]
         */
        $sellerList = db('seller')->where(['selling_id'=>$goods_list[0]['selling_id'],'brand_name'=>$goods_list[0]['brand_name']])->find();
        // dump($sellerList);
        // exit;
        //生成订单规则
        $number = rand(10000,99999).time();
        $goodsOrderList = [];
        $goodsList = [];
        foreach ($goods_list as $key => $value) {
            $goodsOrderList['number'] = $number;
            $goodsOrderList['selling_name'] = $value['selling_name'];
            $goodsOrderList['member_phone'] = $userArr['phone'];
            $goodsOrderList['user_name'] = $addressList['uname'];
            $goodsOrderList['user_phone'] = $addressList['phone'];
            $goodsOrderList['user_address'] = $addressList['address'];
            $goodsOrderList['uid'] = $userList['uid'];
            $goodsOrderList['type'] = '1';
            $goodsOrderList['time'] = date('Y-m-d H:i');
            // $goodsOrderList['num'] = '1';
            $goodsOrderList['total'] = $value['live_unit'] * $userList['num'];
            $goodsOrderList['seller_id'] = $sellerList['seller_id'];
            $goodsOrderList['end_total'] = $value['live_unit'] * $userList['num'];

            $goodsList['goods_name'] = $value['goods_name'];
            $goodsList['com'] = $value['com'];
            $goodsList['num'] = $userList['num'];
            $goodsList['uid'] = $userList['uid'];
            $goodsList['g_id'] = $userList['gid'];
            $goodsList['brand_name'] = $value['brand_name'];
            $goodsList['number'] = $goodsOrderList['number'];
            $goodsList['type'] = '1';
            $goodsList['good_type'] = '1';
            $goodsList['unit'] = $value['live_unit'];
            $goodsList['goods_img'] = $goods_img;


            $end_num['end_num'] = $value['end_num'] + $userList['num'];
            $end_num['final_num'] = $value['num'] - $end_num['end_num'];
            // $goodsList['unit'] = $userList['live_unit'];
            // $goodsList['goods_name'] = $value['goods_name'];
        }

        $success['number'] = $number;
        $order_id = db('goods_order')->insertGetId($goodsOrderList);

        $order_number = db('goods_order')->where(['order_id'=>$order_id])->find();
        if (!empty($order_id)) {
            $goodsList['order_id'] = $order_id;
            $goodsList['number'] = $order_number['number'];
            if (db('goods')->insert($goodsList) > 0) {
                db('activity_goods')->where(['g_id'=>$userList['gid']])->update($end_num);
                echo json_encode(array("status"=>1,'message'=>'生成订单成功!',"data"=>$success));
                exit;
            }else{
                echo json_encode(array("status"=>2,'message'=>'生成订单失败!',"data"=>[]));
                exit;
            }
        }else{
            echo json_encode(array("status"=>2,'message'=>'生成订单失败!',"data"=>[]));
            exit;
        }
        
        // dump($goodsList);
        // exit;
    }




    
}
